//
//  FeedCell.m
//  TableViewCellCustomize
//
//  Created by Kishikawa Katsumi on 10/05/05.
//  Copyright 2010 Kishikawa Katsumi. All rights reserved.
//

#import "FeedCell.h"

static UIFont *badgeFont;

@interface BadgeView : UIView {
	NSInteger width;
	NSInteger badgeNumber;
	
	CGSize numberSize;
	NSString *countString;
	UITableViewCell *cell;
	
	UIColor *badgeColor;
	UIColor *badgeColorHighlighted;
}

@property (readonly) NSInteger width;
@property (nonatomic,retain) UITableViewCell *cell;

@property (nonatomic) NSInteger badgeNumber;
@property (nonatomic, retain) UIColor *badgeColor;
@property (nonatomic, retain) UIColor *badgeColorHighlighted;

@end

@implementation BadgeView

@synthesize width;
@synthesize badgeNumber;
@synthesize cell;
@synthesize badgeColor;
@synthesize badgeColorHighlighted;

+ (void)initialize {
    badgeFont = [[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f] retain];
}

- (id)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		self.backgroundColor = [UIColor clearColor];
	}
	
	return self;
}

- (void)drawRect:(CGRect)rect {
	countString = [NSString stringWithFormat: @"%d", self.badgeNumber];
	[countString retain];
	
	numberSize = [countString sizeWithFont:badgeFont];
	
	width = numberSize.width + 16.0f;
	
	CGRect bounds = CGRectMake(0.0f, 0.0f, numberSize.width + 16.0f, 18.0f);
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	float radius = bounds.size.height / 2.0f;
	
	CGContextSaveGState(context);
	
	if (cell.highlighted || cell.selected) {
		UIColor *col;
		
		if (badgeColorHighlighted) {
			col = badgeColorHighlighted;
		} else {
			col = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.000];
        }
		
		CGContextSetFillColorWithColor(context, [col CGColor]);
	} else {
		UIColor *col;
		
		if (badgeColor) {
			col = badgeColor;
		} else {
			col = [UIColor colorWithRed:0.530 green:0.600 blue:0.738 alpha:1.000];
        }
		
		CGContextSetFillColorWithColor(context, [col CGColor]);
	}
    
	CGContextBeginPath(context);
	CGContextAddArc(context, radius, radius, radius, M_PI / 2 , 3 * M_PI / 2, NO);
	CGContextAddArc(context, bounds.size.width - radius, radius, radius, 3 * M_PI / 2, M_PI / 2, NO);
	CGContextClosePath(context);
	CGContextFillPath(context);
	CGContextRestoreGState(context);
	
	bounds.origin.x = (bounds.size.width - numberSize.width) / 2 + 0.5f;
	
	CGContextSetBlendMode(context, kCGBlendModeClear);
	
	[countString drawInRect:bounds withFont:badgeFont];
}

- (void)dealloc {
	[super dealloc];
	[countString release];
}

@end

@interface FeedCellContentView : UIView {
    FeedCell *cell;
    BOOL highlighted;
}

@end

@implementation FeedCellContentView

- (id)initWithFrame:(CGRect)frame cell:(FeedCell *)tableCell {
    if (self = [super initWithFrame:frame]) {
        cell = tableCell;
        self.opaque = YES;
        self.backgroundColor = cell.backgroundColor;
    }
    
    return self;
}

- (void)dealloc {
    [super dealloc];
}

- (void)drawRect:(CGRect)rect {
    [cell.icon drawAtPoint:CGPointMake(4.0f, 3.0f)];
    
    
    UIView *badgeView = cell.badgeView;
    CGFloat x = badgeView.frame.origin.x;
    if (CGRectEqualToRect(badgeView.frame, CGRectZero)) {
        x = 320.0f;
    }
    
    highlighted ? [[UIColor whiteColor] set] : [[UIColor colorWithWhite:0.300 alpha:1.000] set];
    [cell.title drawInRect:CGRectMake(26.0f, 1.0f, self.frame.size.width - 30.0f - (self.frame.size.width - x + 8.0f), 40.0f) withFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f] lineBreakMode:UILineBreakModeTailTruncation];
}

- (void)setHighlighted:(BOOL)b {
    highlighted = b;
    [self setNeedsDisplay];
}

- (BOOL)isHighlighted {
    return highlighted;
}

@end

@implementation FeedCell

@synthesize icon;
@synthesize title;
@synthesize badgeNumber;
@synthesize badgeColor;
@synthesize badgeColorHighlighted;
@synthesize badgeView;

- (id)initWithStyle:(UITableViewCellStyle)style 
    reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        cellContentView = [[FeedCellContentView alloc] 
                           initWithFrame:CGRectInset(self.contentView.bounds, 
                                                     0.0f, 
                                                     1.0f) 
                           cell:self];
        cellContentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
        UIViewAutoresizingFlexibleHeight;
        cellContentView.contentMode = UIViewContentModeRedraw;
        [self.contentView addSubview:cellContentView];
        [cellContentView release];
        
        badgeView = [[BadgeView alloc] initWithFrame:CGRectZero];
        badgeView.cell = self;
		
        [self.contentView addSubview:badgeView];
		[badgeView release];
		
		[badgeView setNeedsDisplay];
    }
    return self;
}

- (void)layoutSubviews {
	[super layoutSubviews];
    
	if (badgeNumber > 0) {
		[badgeView setHidden:NO];
		
		CGSize badgeSize = 
        [[NSString stringWithFormat:@"%d", badgeNumber] sizeWithFont:badgeFont];
		CGRect badgeframe = 
        CGRectMake(cellContentView.frame.size.width - 
                   (badgeSize.width + 16.0f) - 10.0f, 
                   round((cellContentView.frame.size.height - 18.0f) / 2), 
                   badgeSize.width + 16.0f, 18.0f);
        
        badgeView.frame = badgeframe;
        badgeView.badgeNumber = badgeNumber;
        badgeView.cell = self;
		
		if (self.badgeColorHighlighted) {
			badgeView.badgeColorHighlighted = badgeColorHighlighted;
		} else {
			badgeView.badgeColorHighlighted = [UIColor colorWithRed:1.0f 
                                                              green:1.0f 
                                                               blue:1.0f 
                                                              alpha:1.0f];
        }
		
		if (badgeColor) {
			badgeView.badgeColor = badgeColor;
		} else {
			badgeView.badgeColor = [UIColor colorWithRed:0.530f 
                                                   green:0.600f 
                                                    blue:0.738f 
                                                   alpha:1.0f];
        }
	} else {
		badgeView.hidden = YES;
	}
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    
    [UIView setAnimationsEnabled:NO];
    CGSize contentSize = cellContentView.bounds.size;
    cellContentView.contentStretch = 
    CGRectMake(225.0f / contentSize.width, 
               0.0f, 
               (contentSize.width - 260.0f) / contentSize.width, 
               1.0f);
    [UIView setAnimationsEnabled:YES];
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:backgroundColor];
    cellContentView.backgroundColor = backgroundColor;
}

- (void)setNeedsDisplay {
    [super setNeedsDisplay];
    [cellContentView setNeedsDisplay];
	[badgeView setNeedsDisplay];
}

- (void)setBadgeNumber:(NSInteger)number {
    badgeNumber = number;
    [badgeView setNeedsDisplay];
}

- (void)dealloc {
	[icon release];
	[title release];
    [super dealloc];
}

@end
